package com.kaishengit.service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.WebsiteDao;
import com.kaishengit.pojo.Company;
import com.kaishengit.pojo.Contacts;
import com.kaishengit.pojo.Website;
import com.kaishengit.util.DateUtil;

@Transactional
public class WebsiteService {

	private WebsiteDao websiteDao;
	
	public void savewebsite(String[] website, String[] websitetype,Contacts contacts) {
		websiteDao.delAll(findByObject(contacts));
		if(website != null && websitetype != null) {
			for(int i = 0; i < website.length; i ++) {
				for(int j = 0; j < websitetype.length; j ++) {
					if(i == j) {
						Website site = new Website();
						site.setWebsite(website[i]);
						site.setType(websitetype[j]);
						site.setCreatetime(DateUtil.getNow());
						site.setContacts(contacts);
						websiteDao.save(site);
					}
				}
			}
			
		}
	}

	public WebsiteDao getWebsiteDao() {
		return websiteDao;
	}

	public void setWebsiteDao(WebsiteDao websiteDao) {
		this.websiteDao = websiteDao;
	}

	public void savewebsite(String[] website, String[] websitetype,
			Company company) {
		if(website != null && websitetype != null) {
			for(int i = 0; i < website.length; i ++) {
				for(int j = 0; j < websitetype.length; j ++) {
					if(i == j) {
						Website site = new Website();
						site.setWebsite(website[i]);
						site.setType(websitetype[j]);
						site.setCreatetime(DateUtil.getNow());
						site.setCompany(company);
						websiteDao.save(site);
					}
				}
			}
			
		}
		
	}

	public List<Website> findByObject(Contacts contacts) {
		String hql = "from Website where website.contacts.id = ?";
		return websiteDao.find(hql, contacts.getId());
	}

	public List<Website> findByObject(Company company) {
		String hql = "from Website where website.contacts.id = ?";
		return websiteDao.find(hql, company.getId());
	}

	public void delwebsite(Contacts contacts) {
		websiteDao.delAll(findByObject(contacts));
	}
}

	
	
	
